package com.bulletelc.apollopre.module.display.product.details.comment;

import com.bulletelc.apollopre.MyApplication;
import com.bulletelc.apollopre.constants.GlobalConfig;
import com.bulletelc.apollopre.constants.NetWorkPath;
import com.bulletelc.apollopre.entity.CommentBean;
import com.bulletelc.apollopre.framework.BasePresenter;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * author: lhl
 * date  : 2018/01/15
 * desc  : TODO
 */

public class CommentPresenter extends BasePresenter<CommentContract.View> implements CommentContract.Presenter {
    @Override
    public void start() {

    }

    @Override
    public void stop() {

    }

    @Override
    public void getCommentsList(String productId) {

        getComments(productId);
    }

    /**
     * 获取商品的评论数据
     * @param productId
     */
    public void getComments(String productId) {
        OkGo.<String>post(NetWorkPath.GET_PRODUCT_COMMENT+"?id="+productId)
                .params("access_token", MyApplication.getToken())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        parseComments(response);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        if (isViewAttached()) {
                            getView().onErrorCommentsList(response.getException().toString());
                        }
                    }
                });
    }

    public void parseComments(Response<String> response) {
        if(response.isSuccessful()) {
            try {
                JSONObject obj = new JSONObject(response.body().toString());
                JSONObject objMeta = new JSONObject(obj.getString("meta"));
                if(GlobalConfig.HTTP_RESULT_SUCCESS.equals(objMeta.getString("code"))) {
                    JSONArray objSub = new JSONArray(obj.getString("data"));
                    List<CommentBean> comments = new ArrayList<>();
                    for (int i=0; i<objSub.length();i++) {
                        CommentBean cb = new CommentBean();
                        cb.setId(objSub.getJSONObject(i).getString("id"));
                        cb.setAvatar(objSub.getJSONObject(i).getString("avatar"));
                        cb.setUsername(objSub.getJSONObject(i).getString("username"));
                        cb.setTotal_mark(objSub.getJSONObject(i).getString("total_mark"));
                        cb.setContent(objSub.getJSONObject(i).getString("content"));
                        cb.setCreated_at(objSub.getJSONObject(i).getString("created_at"));
                        cb.setIs_anonymous(objSub.getJSONObject(i).getInt("is_anonymous"));
                        cb.setProduct_sku_name(objSub.getJSONObject(i).getString("product_sku_name"));

                        //评论图片数据
                        JSONArray photosArray = objSub.getJSONObject(i).getJSONArray("photos");
                        //LogUtil.i(photosArray.toString());
                        List<String> photos = new ArrayList<>();
                        int length = photosArray.length();
                        for (int j = 0; j < length; j++) {
                            String src = photosArray.getJSONObject(j).getString("src");
                            photos.add(src);
                        }
                        cb.setPhotos(photos);

                        comments.add(cb);
                    }
                    if(isViewAttached()) {
                        getView().onSuccessCommentsList(comments);
                    } else {
                        if (isViewAttached()) {
                            getView().onFailCommentsList(objMeta.getString("msg"));
                        }
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            if(isViewAttached()) {
                getView().onFailCommentsList(response.body().toString());
            }
        }

    }
}
